//
//  XS_CouponItemCell.swift
//  XueDao
//
//  Created by Mac on 2020/12/26.
//

import UIKit

class XS_CouponItemCell: UICollectionViewCell {
    
    var _model : XS_CouponItemModel!
    public var model : XS_CouponItemModel!{
        set{
            _model = newValue
            //设置数据
            let priceAttStr = NSMutableAttributedString.init(string: "￥" + newValue.money)
            priceAttStr.addAttributes([.font : UIFont.systemFont(ofSize: 12, weight: .semibold)], range: NSRange.init(location: 0, length: 1))
            priceLabel.attributedText = priceAttStr
            conditionLabel.text = "满" + newValue.condition + "可用"
            nameLabel.text = newValue.name
            contentLabel.text = newValue.remark
            selectBtn.isSelected = newValue.selected
            
            let endTime = newValue.endTime
            let dateFormater = DateFormatter.init()
            dateFormater.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let endDate = dateFormater.date(from: endTime)
            _ = Date.init()
            let time = endDate?.timeIntervalSinceNow ?? 0
            let day = Int(time) / 3600 / 24
            let hourStr = Int(time) / 3600 % 24
            let minuteStr = Int(time) / 60 % 60
            let timeStr = day > 0 ? "\(day)天" : (hourStr > 0 ? "\(hourStr)小时" : "\(minuteStr)分")
            
            if model.state == "used" {
                timeLabel.text = "已使用"
                expireImageView.image = UIImage.init(named: "已使用_1")
                expireImageView.isHidden = false
                
                let expireColor = UIColor.qmui_color(withHexString: "B9C0C6")
                nameLabel.textColor = expireColor
                timeLabel.textColor = expireColor
                contentLabel.textColor = expireColor
                leftImageView.image = UIImage.init(named: "bgyouhuiquan_l")
            } else {
                timeLabel.text = time > 0 ? "还剩 " + timeStr + " 过期" : endTime + " 过期"
                expireImageView.image = UIImage.init(named: "已过期_1")
                expireImageView.isHidden = time > 0
                
                if time > 0 {
                    nameLabel.textColor = UIColor.qmui_color(withHexString: "383C3F")
                    timeLabel.textColor = UIColor.qmui_color(withHexString: "72777C")
                    contentLabel.textColor = UIColor.qmui_color(withHexString: "AAB1B7")
                    leftImageView.image = UIImage.init(named: "bgyouhuiquan_n")
                } else {
                    let expireColor = UIColor.qmui_color(withHexString: "B9C0C6")
                    nameLabel.textColor = expireColor
                    timeLabel.textColor = expireColor
                    contentLabel.textColor = expireColor
                    leftImageView.image = UIImage.init(named: "bgyouhuiquan_l")
                }
            }
        }
        get{
            return _model
        }
    }
    
    var _canSelect : Bool = false
    public var canSelect : Bool{
        set{
            _canSelect = newValue
            selectBtn.isHidden = !newValue
        }
        get{
            return _canSelect
        }
    }

    @IBOutlet weak var priceLabel: UILabel!
    @IBOutlet weak var conditionLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var expireImageView: UIImageView!
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var selectBtn: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        selectBtn.isUserInteractionEnabled = false
    }

}
